using System;
using Oracle.ManagedDataAccess.Client;
using OracleInternal.Common;

namespace OracleInternal.TTC
{
	internal class TTCDescribeInfo
	{
		internal static void ReadMessage(bool bForDescribe, bool bForRefCursor, MarshallingEngine mEngine, SQLMetaData sqlMetaData, bool bIgnoreMetadata)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCDescribeInfo, OracleTraceFuncName.ReadMessage);
			}
			bIgnoreMetadata = false;
			try
			{
				if (!bForDescribe)
				{
					int num = mEngine.UnmarshalUB1();
					if (!bForRefCursor && num > 0)
					{
						sqlMetaData.m_describeInfoHash = mEngine.GetNBytes(num);
					}
					if (bIgnoreMetadata)
					{
						mEngine.UnmarshalUB4(bIgnoreData: true);
					}
					else
					{
						sqlMetaData.m_maxRowSize = (int)mEngine.UnmarshalUB4();
					}
				}
				if (bForDescribe)
				{
					if (bIgnoreMetadata)
					{
						mEngine.UnmarshalUB2(bIgnoreData: true);
					}
					else
					{
						sqlMetaData.m_noOfColumns = (short)mEngine.UnmarshalUB2();
					}
				}
				else
				{
					if (bIgnoreMetadata)
					{
						mEngine.UnmarshalUB4(bIgnoreData: true);
					}
					else
					{
						sqlMetaData.m_noOfColumns = (short)mEngine.UnmarshalUB4();
					}
					if (sqlMetaData.m_noOfColumns > 0)
					{
						mEngine.UnmarshalUB1(bIgnoreData: true);
					}
				}
				if (!bIgnoreMetadata)
				{
					if (sqlMetaData.m_columnDescribeInfo == null)
					{
						sqlMetaData.m_columnDescribeInfo = new ColumnDescribeInfo[sqlMetaData.m_noOfColumns];
					}
					else if (sqlMetaData.m_columnDescribeInfo.Length != sqlMetaData.m_noOfColumns)
					{
						throw new Exception("Internal Error");
					}
				}
				for (int i = 0; i < sqlMetaData.m_noOfColumns; i++)
				{
					if (!bIgnoreMetadata && sqlMetaData.m_columnDescribeInfo[i] == null)
					{
						sqlMetaData.m_columnDescribeInfo[i] = new ColumnDescribeInfo();
					}
					TTCColumnInfo.ReadMessage(mEngine, sqlMetaData.m_columnDescribeInfo[i], bIgnoreMetadata);
					if (sqlMetaData.m_columnDescribeInfo[i].m_dataType == 119)
					{
						sqlMetaData.m_numOfJSONColumns++;
						sqlMetaData.m_bHasJSONColumn = true;
					}
				}
				if (!bForDescribe)
				{
					mEngine.UnmarshalDALC(bIgnoreData: true);
					if (mEngine.NegotiatedTTCVersion >= 3)
					{
						mEngine.UnmarshalUB4(bIgnoreData: true);
						mEngine.UnmarshalUB4(bIgnoreData: true);
						if (mEngine.NegotiatedTTCVersion >= 4)
						{
							mEngine.UnmarshalUB4(bIgnoreData: true);
							mEngine.UnmarshalUB4(bIgnoreData: true);
							if (mEngine.NegotiatedTTCVersion >= 5)
							{
								mEngine.UnmarshalDALC(bIgnoreData: true);
							}
						}
					}
				}
				sqlMetaData.bGotDescribeInfoFromDB = true;
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCDescribeInfo, OracleTraceFuncName.ReadMessage, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCDescribeInfo, OracleTraceFuncName.ReadMessage);
				}
			}
		}
	}
}
